package com.lianzhihui.romeex.ui.wallet;

import android.content.Intent;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.bean.ProfitFindHomeBean;
import com.lianzhihui.romeex.bean.WalletMainBean;
import com.lianzhihui.romeex.inter.DialogListener;
import com.lianzhihui.romeex.mvp.presenter.HuaZhuanPresnterImp;
import com.lianzhihui.romeex.mvp.view.HuaZhuanActivityView;
import com.lianzhihui.romeex.ui.base.BaseActivity;
import com.lianzhihui.romeex.utils.CoinUtils;
import com.lianzhihui.romeex.widget.dialog.HuaZhuanSucDialog;
import com.lianzhihui.romeex.widget.dialog.PasswordDialog;

import java.text.MessageFormat;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description: 划转业务
 * Author: RAMON
 * CreateDate: 2020/11/6 6:41 PM
 * UpdateUser:
 * UpdateDate:
 * UpdateRemark:
 * Version:
 */
public class HuaZhuanActivty extends BaseActivity implements HuaZhuanActivityView {

    @BindView(R.id.include_top_iv_back)
    ImageView includeTopIvBack;
    @BindView(R.id.include_top_lin_newback)
    LinearLayout includeTopLinNewback;
    @BindView(R.id.include_top_tv_title)
    TextView includeTopTvTitle;
    @BindView(R.id.include_top_iv_right_second)
    ImageView includeTopIvRightSecond;
    @BindView(R.id.include_top_tv_right)
    TextView includeTopTvRight;
    @BindView(R.id.include_top_lin_right)
    LinearLayout includeTopLinRight;
    @BindView(R.id.include_top_lin_background)
    LinearLayout includeTopLinBackground;
    @BindView(R.id.app_top_bar)
    LinearLayout appTopBar;
    @BindView(R.id.iv_exchange)
    ImageView ivExchange;
    @BindView(R.id.et_sum)
    EditText etSum;
    @BindView(R.id.tv_unit)
    TextView tvUnit;
    @BindView(R.id.tv_suoha)
    TextView tvSuoha;
    @BindView(R.id.tv_canuse)
    TextView tvCanuse;
    @BindView(R.id.tv_balance)
    TextView tvBalance;
    @BindView(R.id.exchange_from)
    TextView exchangeFrom;
    @BindView(R.id.exchange_to)
    TextView exchangeTo;
    @BindView(R.id.bt_submit)
    Button btSubmit;
    private HuaZhuanPresnterImp huaZhuanModelImp;
    WalletMainBean.CoinBean coin;
    private String validBalance;
    private String profitBalance;
    private String uidQuota;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_huazhuan_layout;
    }

    @Override
    protected void initView() {
        setTitle(getString(R.string.huazhuan));
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        coin = (WalletMainBean.CoinBean) intent.getSerializableExtra("coin");
        if (coin == null) {
            coin = CoinUtils.getDefCoin();
        }
        huaZhuanModelImp = new HuaZhuanPresnterImp(this, this);
        huaZhuanModelImp.requestFindHome(coin.getCoinNameCobo(), coin.getCoinName());
    }


    @OnClick({R.id.iv_exchange, R.id.bt_submit, R.id.tv_suoha})
    public void OnClick(View v) {
        switch (v.getId()) {
            case R.id.iv_exchange:
                //交换按钮
                if (exchangeFrom.getText().toString().equals(getResources().getString(R.string.text_earings_returns_the_balance))) {
                    exchangeFrom.setText(R.string.text_earings_available_balance);
                    exchangeTo.setText(R.string.text_earings_returns_the_balance);
                    tvCanuse.setText(MessageFormat.format("{0}{1}", validBalance, getResources().getString(R.string.text_rex)));
                } else {
                    exchangeFrom.setText(R.string.text_earings_returns_the_balance);
                    exchangeTo.setText(R.string.text_earings_available_balance);
                    tvCanuse.setText(MessageFormat.format("{0}{1}", profitBalance, getResources().getString(R.string.text_rex)));
                }
                break;
            case R.id.tv_suoha:
                //全部按钮
                if (exchangeFrom.getText().toString().equals(getResources().getString(R.string.text_earings_returns_the_balance))) {
                    //收益-》可用
                    etSum.setText(profitBalance);
                } else {
                    //可用-》收益
                    etSum.setText(validBalance);

                }
                break;
            case R.id.bt_submit:
                //确认
                //正则：有1~8位小数的正实数

                String pat = "^[0-9]+(\\.[0-9]{1,8})?$";
                String inPutStr = etSum.getText().toString();
                boolean matches = Pattern.matches(pat, inPutStr);
                if ((etSum.getText().toString().trim().isEmpty() || !matches)||Double.parseDouble(etSum.getText().toString().trim())<=0) {
                    toast(getString(R.string.text_correct_money));
                    return;
                }

                long value = (long) (Double.parseDouble(inPutStr) * 100000000L);
                String amount = String.valueOf(value);
                if (exchangeFrom.getText().toString().equals(getResources().getString(R.string.text_earings_returns_the_balance))) {
                    //收益-》可用
                    if (Double.parseDouble(inPutStr) > Double.parseDouble(profitBalance)) {
                        toast(getString(R.string.text_not_enough));
                        return;
                    }
                    if(Double.parseDouble(inPutStr) > Double.parseDouble(uidQuota)){
                        toast(getString(R.string.text_quota_enough));
                        return;
                    }
                } else {
                    //可用-》收益
                    if (Double.parseDouble(inPutStr) > Double.parseDouble(validBalance)) {
                        toast(getString(R.string.text_not_enough));
                        return;
                    }

                }


                PasswordDialog dialog = new PasswordDialog(this, getResources().getString(R.string.text_huazhuan_security_verification));
                dialog.setTitleCenterHorizontal(Gravity.CENTER_HORIZONTAL);
                dialog.setListener(new DialogListener() {
                    @Override
                    public void onPassed() {
                        showLoading("");
                        if (exchangeFrom.getText().toString().equals(getResources().getString(R.string.text_earings_returns_the_balance))) {
                            //收益-》可用
                            huaZhuanModelImp.requestTransfer(coin.getCoinNameCobo(), coin.getCoinName(), amount, "1");
                        } else {
                            //可用-》收益
                            huaZhuanModelImp.requestTransfer(coin.getCoinNameCobo(), coin.getCoinName(), amount, "2");
                        }
                        dialog.dismiss();
                    }

                    @Override
                    public void onFail() {

                    }
                });
                dialog.show();


                break;
            default:
                break;
        }
    }

    @Override
    public void responseFindHomeSuccess(ProfitFindHomeBean data) {
        //可用
        validBalance = data.getValidBalance();
        //收益
        profitBalance = data.getProfitBalance();
        //余额
        uidQuota = data.getUidQuota();
        tvCanuse.setText(MessageFormat.format("{0}{1}", profitBalance, getResources().getString(R.string.text_rex)));
        //余额
        tvBalance.setText(MessageFormat.format("{0}{1}", uidQuota, getResources().getString(R.string.text_rex)));
    }


    /**
     * 划转成功
     */
    @Override
    public void responseTransSuccess() {
        dismissLoading();
        HuaZhuanSucDialog dialog = new HuaZhuanSucDialog(this);
        dialog.show();

    }


    @Override
    public void setFailure() {
        dismissLoading();
    }
}
